// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------
// Modification history:
// feature: content filter topic
// ------------------------------------------------------------------

#include "DDSFilterExpression.hpp"

#include <map>
#include <memory>
#include <string>
#include <vector>

#include <ecdr/Cdr.h>
#include <ecdr/FastBuffer.h>
#include <ecdr/exceptions/Exception.h>

#include <edds/rtps/interfaces/IContentFilter.hpp>
#include <ertps/types/DynamicDataFactory.h>
#include <ertps/types/DynamicTypeBuilderFactory.h>
#include <rtps/content_filter/DDSSQLFilter/DDSFilterFactory.hpp>
#include "DDSFilterConditionState.hpp"
#include "DDSFilterField.hpp"
#include "DDSFilterParameter.hpp"
#include <ertps/types/DynamicData.h>

namespace evbs {
namespace ertps {
namespace rtps {
namespace DDSSQLFilter {

using namespace ertps::types;

bool DDSFilterExpression::evaluate(void* data) const {
    root->reset();
    for (auto it = fields.begin(); it != fields.end() && DDSFilterConditionState::UNDECIDED == root->get_state();
         ++it) {
        if (!it->second->set_value(static_cast<evbs::ertps::types::DynamicData*>(data))) {
            return false;
        }
    }
    return DDSFilterConditionState::RESULT_TRUE == root->get_state();
}

void DDSFilterExpression::clear() {
    parameters.clear();
    fields.clear();
    root.reset();
}

}  // namespace DDSSQLFilter
}  // namespace rtps
}  // namespace ertps
}  // namespace evbs
